package QQClient;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public final class EncodeUtil {

	public Object bytes2Object(byte[] objBytes) throws Exception {  
	    if (objBytes == null || objBytes.length == 0) {  
	        return null;  
	    }  
	    ByteArrayInputStream bi = null;
	    ObjectInputStream oi = null;

	    try{
		    bi = new ByteArrayInputStream(objBytes);  
		    oi = new ObjectInputStream(bi);  
		    
		    return oi.readObject();
	    }finally{
	    	try{if(bi!=null)bi.close();}catch(Exception e1){}
	    	try{if (oi!=null)oi.close();}catch(Exception e2){}
	    }
	}  
	  
	public byte[] object2Bytes(Serializable obj) throws Exception {  
	    if (obj == null) {  
	        return null;  
	    }  
	    ByteArrayOutputStream bo = null;
	    ObjectOutputStream oo  = null;
	    try{
		    bo = new ByteArrayOutputStream();  
		    oo = new ObjectOutputStream(bo);  
		    oo.writeObject(obj);  
		    return bo.toByteArray();
	    }finally{
	    	try{if(bo!=null)bo.close();}catch(Exception e1){}
	    	try{if (oo!=null)oo.close();}catch(Exception e2){}
	    }
	}  

}
